package com.gsj.boot.service.impl;

/**
 * @program: mybatisDriud
 * @description:
 * @author: 郭书江
 * @create: 2022-06-29 09:19
 **/
import com.gsj.boot.mapper.UserMapper;
import com.gsj.boot.model.User;
import com.gsj.boot.model.UserTb;
import com.gsj.boot.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@CacheConfig(cacheNames = "user")
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    @Cacheable(value = "users",key = "'allUsers'", unless = "#result==null")
    public List<User> getAll() {
        List<User>  all= userMapper.getAll();
        return all;
    }

    @Override
    public User getOne(Long id) {
        return null;
    }

    @Override
    public int insert(User user) {
        int insert = userMapper.insert(user);
        return insert;
    }

    @Override
    @CacheEvict(key = "#id")
    public int delete(Long id) {
        int delete = userMapper.delete(id);
        return delete;
    }

    @Override
    @CachePut(key = "#user.userName")
    public int update(String userName, String passWord) {
        int update = userMapper.update(userName, passWord);
        return update;
    }

    @Override
    @Cacheable(value ="userTb" ,key = "'bigUserData'", unless = "#result==null")
    public List<UserTb> getBigData() {
        List<UserTb> bigData = userMapper.getBigData();
        return bigData;
    }


}